/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.huawei.spare.part.replace.views.replace.load;

import com.huawei.spare.part.replace.framework.SmartContentPresenter;
import com.huawei.spare.part.replace.framework.SmartDataBus;
import com.huawei.spare.part.replace.framework.SmartRouter;
import com.huawei.spare.part.replace.models.Project;
import com.huawei.spare.part.replace.models.Replacement;
import com.huawei.spare.part.replace.utils.Application;
import com.huawei.spare.part.replace.utils.JsonMapper;
import com.huawei.spare.part.replace.views.widgets.Alerts;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.stage.FileChooser;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Woo
 */
public class LoadPresenter extends SmartContentPresenter {

    @Override
    public void initialize() {
        Platform.runLater(() -> {
            SmartRouter.get().nextStepButton().setDisable(true);
            if (SmartDataBus.get().getAutoLoad()) {
                SmartDataBus.get().setAutoLoad(false);
                loadProjectFile(SmartDataBus.get().getProject().getSaveTo());
            }
        });
    }

    @FXML
    private void loadProject(ActionEvent event) {
        FileChooser chooser = new FileChooser();
        chooser.setTitle(getString("load.project.chooser.title"));
        final File projectFolder = new File(Application.getProject());
        if (!projectFolder.exists())
            projectFolder.mkdirs();
        chooser.setInitialDirectory(projectFolder);
        final FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(getString("load.project.chooser.desc"), "*.json");
        chooser.getExtensionFilters().add(extFilter);
        File file = chooser.showOpenDialog(SmartDataBus.get().getStage());
        if (file != null) {
            loadProjectFile(file);
        }
    }

    private void loadProjectFile(File file) {
        try {
            final Project prepared = loadProject(file);

            final File folder = file.getParentFile();
            final List<String> collect = Arrays.stream(folder.listFiles()).map(File::getName).collect(Collectors.toList());
            final boolean isAllPackageReady = collect.containsAll(prepared.getDownloadPackages().getAllPackageNames());

            if (!isAllPackageReady) {
                Alerts.error("load.project.validation.package.missing", "load.project.validation.package.missing.content");
            } else {
                SmartRouter.get().routeToNextView();
            }
        } catch (FileNotFoundException e) {
            // should not happen
        }
    }

    public static Project loadProject(File file) throws FileNotFoundException {
        final Project prepared = JsonMapper.nonDefaultMapper()
                                           .getBean(new FileInputStream(file), Project.class);
        final Replacement replacement = SmartDataBus.get().replacement();
        replacement.setProjectFile(file);
        replacement.setProject(prepared);
        return prepared;
    }


}
